/**
 * @class $$.data.Tree
 */
$$.data.Tree = (function(){
    /**
     * iterator
     * @param {Array} arr
     * @param {Object} scope
     * @param {Number} i
     * @param {Number} j A ghost value, 穿梭于
     */
    function array_Iterator(arr, scope, i, j){
        // init...
        if(typeof scope == 'undefined'){
            scope = this;
        }
        
        if(typeof i == 'undefined'){
            i = 0;
        }
        if(typeof j == 'undefined'){
            j = 0;
        }
        // init end.
        
        var elmemnt = arr[i];
        
        if(typeof elmemnt == 'undefined'){
            return;
        }else{
            // do something...
            if(array_Iterator.Fn){
                if(array_Iterator.Fn.call(scope, elmemnt, arr, i, j) == false){
                    return;
                }
            };
            // do something-End
            if(elmemnt.children && elmemnt.children.length){
//                var x = j + 1; // 每一次循环children都会对j产生变化。
                // -->y
                array_Iterator(elmemnt.children, elmemnt/*已经变了这个Scope*/, 0, j + 1);

            };
            // -->x
            array_Iterator(arr, scope, ++i, j /* Do not give a "j" here, cause it's needed to have j reset!*/);
        }
    }
	
return {
    array_Iterator : function(fn, output){
    	var list = [];
    	array_Iterator.Fn = fn;
    	array_Iterator(this.aspArray(), list);
    }
	
	/**
	 * @return {Array}
	 */
    ,toList : function(){
        var list = [];
        array_Iterator.Fn = function(e, arr, i, j){
            list.push(e);
        }
        
        array_Iterator(this.tree, list);
        return list;
    }
    
    /**
     * @return {Object}
     */
    ,level : function(level){
        var result = [];
        array_Iterator.Fn = function(e, arr, i, j){
        	if(j == level){
	            // assert every node has a key name called "id"
				if(!this.fullPath)this.fullPath = ''; // 执行此步的时候为根目录
				e.fullPath = this.fullPath + '/' + e.id;
	            result.push(e);
        	}
        }
        
        array_Iterator(this.tree, result);
        return result;
    }
    
    
    /**
     * @return {Object}
     */
    ,_toHash : function(){
        var hash = {};
        array_Iterator.Fn = function(e, arr, i, j){
			if(!this.fullPath)this.fullPath = ''; // 执行此步的时候为根目录
//			// 包含父路径的全称路径
			e.fullPath = this.fullPath + '/' + e.id;
//			// 缩进
			e.sj = j;
			
			/*
			 * @todo
			 * 服务器的错！！！！！！！！！！！！！！！！！！！！不能读取Array！？？？ 
			 */
			var obj;
			obj = {
			     text		: e.text
			    ,id			: e.id
			    ,fullPath	: e.fullPath
			    ,sj			: e.sj
            };
            if(e.children){
            	for(var arr = [], i = 0, j = e.children.length; i < j; i++){
            		arr[i] = e.children[i];
            	}
//            	arr = [].concat(e.children); 快速克隆数组无效！？
            	obj.children = arr;
            }
            
			/*
			 * 服务器的错！！！！！！！！！！！！！！！！！！！！不能读取Array！？？？ 
			 */
            this[e.id] = obj || e;
        }
        
        array_Iterator(this.tree, hash);
        
        return hash;
    }
    ,xx : function(data){
        var hash = {};
        array_Iterator.Fn = function(e, arr, i, j){
			if(!this.fullPath)this.fullPath = ''; // 执行此步的时候为根目录
//			// 包含父路径的全称路径
			e.fullPath = this.fullPath + '/' + e.id;
//			// 缩进
			e.sj = j;
            this[e.id] = e;
        }
        
        array_Iterator(data, hash);
        
        return hash;
    }
    
    /**
     * getHashValueByFullKey
     * @param 	{Boolean} isAllMatch
     * @param   {String|Array}
     * @return	{Object}
     */
    ,hashSearch : function(isAllMatch, list){
        var 
              result = []
             ,i		 = 0;
            
        if(!list && arguments.length > 1){			// 可变参数的条件
        	list = Array.prototype.slice.call(arguments, 1);
        }else if(list && typeof list == 'string'){	// 送入字符串的条件
        	list = list.split('/');	
        	list.shift();
        }else if(list && list.pop){					// 送入数组的条件
        	// do nothing...
        }
//        debugger;
//        if(!this.hash){
//        	this.hash = this._toHash();
//        }

        /**
         * @param {String} key
         */
        (function(key){
            var _shift = this[key];
            if(!_shift){
                return;
            }else{
                result.push(_shift);
                i++;
                var nextKey = list[i];
                arguments.callee.call(_shift, nextKey);
            }
        }).call(this.xx(this.aspArray()), list[i]);
        
        return isAllMatch ? result : result.pop();
    }
    
    /**
     * 	可编织任意的数据，如：      
	    var h = {};
        var foo = this.webStructure.buildIndex(null, null, function(e, arr, i, j){
			if(j == 1){
	            h[e.id] = e;
			}
        });
     * @param {Array} arr
     * @param {String} keyName 注意不是什么类型的数据都可以作为key！
     * @param {Function} handler
     * @return {Object}
     */
    ,buildIndex : function(arr, keyName, handler){
        var hash;
        
        hash	= {}
    	arr 	= arr		|| this.tree
    	keyName = keyName	|| 'id';  // assert every node has a key name called "id"
    	
        array_Iterator.Fn = handler;
        
        array_Iterator(arr, hash);
        
        return hash;
    }
};
})(); 